﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace SPM.ResoureGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("SPM.ResourceGenerator.exe <resx file> <namespace>");
                return;
            }

            string resxFile = args[0];
            string ns = args[1];

            if (File.Exists(resxFile) == false)
            {
                Console.WriteLine("Could not find file {0}", resxFile);
                return;
            }

            Console.WriteLine("Generating SharePoint resources for {0}", resxFile);

            string path = Path.GetDirectoryName(resxFile);
            string noExtension = Path.GetFileNameWithoutExtension(resxFile);
            string sourceFile = Path.Combine(path, noExtension + ".Designer.cs");

            XmlDocument resx = new XmlDocument();
            resx.Load(resxFile);

            using (StreamWriter sw = new StreamWriter(sourceFile))
            {
                sw.WriteLine("using System;");
                sw.WriteLine("using Microsoft.SharePoint.Utilities;");
                sw.WriteLine("");
                sw.WriteLine("namespace {0}", ns);
                sw.WriteLine("{");
                sw.WriteLine("\tpublic class {0}_Resources", CreateClassName(noExtension));
                sw.WriteLine("\t{");

                foreach (XmlNode node in resx.SelectNodes("/root/data"))
                {
                    string name = node.Attributes["name"].Value;

                    sw.WriteLine("\t\tpublic static string {0}", CreatePropertyName(name));
                    sw.WriteLine("\t\t{");
                    sw.WriteLine("\t\t\tget");
                    sw.WriteLine("\t\t\t{");
                    sw.WriteLine("\t\t\t\treturn GetString(\"{0}\");",name);

                    //sw.WriteLine("\t\t\t\treturn SPUtility.GetLocalizedString(\"$Resources:{1}\",\"{0}\",(uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);",
                    //    noExtension, name);
                    sw.WriteLine("\t\t\t}");
                    sw.WriteLine("\t\t}");
                }

                sw.WriteLine("\t\tpublic static string GetString(string key)");
                sw.WriteLine("\t\t{");
                sw.WriteLine("\t\t\tuint lcid = (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID;");
                sw.WriteLine("\t\t\tstring resourceString = string.Format(\"$Resources:{0}\",key);");
                sw.WriteLine("\t\t\treturn SPUtility.GetLocalizedString(resourceString,\"{0}\",lcid);", noExtension);
                sw.WriteLine("\t\t}");

                sw.WriteLine("\t}"); // end class
                sw.WriteLine("}"); // end namespace
            }
        }

        private static string CreateClassName(string name)
        {
            return name.Replace(".", "_");
        }
        private static string CreatePropertyName(string name)
        {
            return name.Replace(".", "_");
        }
    }
}
